package com.shopAndMall.AppBuilder;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.util.Log;

public class ImageDown extends Activity {
	
	void DownloadFromUrl(String imgUrl, String imgName ) {	
		
		String fileName = imgName;
		
		try {
			//File file = new File(fileName);
			URL url = new URL(imgUrl);
			URLConnection conn = url.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
 
			BufferedInputStream bis = new BufferedInputStream(is);
			ByteArrayBuffer baf = new ByteArrayBuffer(50);
 
			int current = 0;
 
			while ((current = bis.read()) != -1) {
				baf.append((byte) current);
			}
 
			FileOutputStream fos = openFileOutput(fileName, 0);
			fos.write(baf.toByteArray());
			fos.close();
			Log.e("image down","success"); 
			
		} catch (Exception e) {
			Log.e("image down","Failed"); 
		}
	}
	
	public void removeFiles()
    {
    	String mPath = "/data/data/com.greenweb.craigslistXv1/files/";
    	File dir = new File(mPath);

    	String[] children = dir.list();
    	if (children != null) {
    	   for (int i=0; i<children.length; i++) {
    	      String filename = children[i];
    	       File f = new File(mPath + filename);    	
    	       if (f.exists()) {
    	          f.delete();
    	       }
    	   }
    	}
    }
	
}